/*     */ package com.zimbra.cs.wiki;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.common.util.CliUtil;
/*     */ import com.zimbra.cs.db.DbPool;
/*     */ import com.zimbra.cs.db.DbPool.DbConnection;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.store.MailboxBlob;
/*     */ import com.zimbra.cs.store.StoreManager;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.PrintStream;
/*     */ import java.sql.PreparedStatement;
/*     */ import java.sql.ResultSet;
/*     */ import java.sql.SQLException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class WikiDigestFixup
/*     */ {
/*     */   private static StoreManager sStore;
/*     */   
/*     */   private static class Mbox
/*     */   {
/*     */     private int mId;
/*     */     private String mEmail;
/*     */     
/*     */     public Mbox(int id, String email)
/*     */     {
/*  53 */       this.mId = id;
/*  54 */       this.mEmail = email; }
/*     */     
/*  56 */     public int getId() { return this.mId; }
/*  57 */     public String getEmail() { return this.mEmail; }
/*     */   }
/*     */   
/*     */   private static List<Mbox> getMboxList(DbPool.DbConnection conn) throws SQLException {
/*  61 */     List<Mbox> list = new ArrayList(1000);
/*  62 */     String sql = "SELECT id, comment FROM mailbox ORDER BY id";
/*  63 */     PreparedStatement stmt = null;
/*  64 */     ResultSet rs = null;
/*     */     try {
/*  66 */       stmt = conn.prepareStatement(sql);
/*  67 */       rs = stmt.executeQuery();
/*  68 */       while (rs.next()) {
/*  69 */         int id = rs.getInt(1);
/*  70 */         String email = rs.getString(2);
/*  71 */         list.add(new Mbox(id, email));
/*     */       }
/*     */     } finally {
/*  74 */       if (rs != null) rs.close();
/*  75 */       if (stmt != null) stmt.close();
/*     */     }
/*  77 */     return list;
/*     */   }
/*     */   
/*     */   private static class WikiDigest {
/*     */     private int mMboxId;
/*     */     private int mItemId;
/*     */     private String mDigest;
/*     */     
/*     */     public WikiDigest(int mboxId, int itemId, String digest) {
/*  86 */       this.mMboxId = mboxId;
/*  87 */       this.mItemId = itemId;
/*  88 */       this.mDigest = digest;
/*     */     }
/*     */     
/*  91 */     public int getMboxid() { return this.mMboxId; }
/*  92 */     public int getItemId() { return this.mItemId; }
/*  93 */     public String getDigest() { return this.mDigest; }
/*     */   }
/*     */   
/*     */   private static List<WikiDigest> getWikiDigests(int mboxId) throws IOException, ServiceException {
/*  97 */     Mailbox mbox = null;
/*     */     try {
/*  99 */       mbox = MailboxManager.getInstance().getMailboxById(mboxId);
/*     */     } catch (ServiceException e) {
/* 101 */       String code = e.getCode();
/* 102 */       if (("account.NO_SUCH_ACCOUNT".equals(code)) || ("service.WRONG_HOST".equals(code)))
/*     */       {
/* 104 */         return null;
/*     */       }
/* 106 */       throw e;
/*     */     }
/* 108 */     OperationContext octxt = new OperationContext(mbox);
/* 109 */     List<MailItem> items = new ArrayList();
/* 110 */     List<MailItem> wikis = mbox.getItemList(octxt, MailItem.Type.WIKI);
/* 111 */     if ((wikis != null) && (wikis.size() > 0))
/* 112 */       items.addAll(wikis);
/* 113 */     List<MailItem> documents = mbox.getItemList(octxt, MailItem.Type.DOCUMENT);
/* 114 */     if ((documents != null) && (documents.size() > 0))
/* 115 */       items.addAll(documents);
/* 116 */     int len = items.size();
/* 117 */     List<WikiDigest> list = new ArrayList(len);
/* 118 */     if (len > 0) {
/* 119 */       for (MailItem item : items)
/*     */       {
/* 121 */         if (item.getSize() <= 2147483647L)
/*     */         {
/* 123 */           int id = item.getId();
/* 124 */           MailboxBlob blob = sStore.getMailboxBlob(item);
/* 125 */           InputStream is = null;
/*     */           try {
/* 127 */             is = sStore.getContent(blob);
/* 128 */             byte[] data = ByteUtil.getContent(is, (int)item.getSize());
/* 129 */             String digest = ByteUtil.getSHA1Digest(data, true);
/* 130 */             String currentDigest = item.getDigest();
/* 131 */             if (!digest.equals(currentDigest)) {
/* 132 */               System.out.println("Found id " + id + ", current digest = \"" + currentDigest + "\"");
/* 133 */               WikiDigest wd = new WikiDigest(mboxId, id, digest);
/* 134 */               list.add(wd);
/*     */             } else {
/* 136 */               System.out.println("Found id " + id + " but skipping because digest is correct.");
/*     */             }
/*     */           } finally {
/* 139 */             ByteUtil.closeStream(is);
/*     */           }
/*     */         } }
/*     */     }
/* 143 */     return list;
/*     */   }
/*     */   
/*     */   private static void fixupItems(DbPool.DbConnection conn, int mboxId, List<WikiDigest> digests) throws SQLException, ServiceException
/*     */   {
/* 148 */     StringBuilder sql = new StringBuilder("UPDATE ");
/* 149 */     sql.append("mailbox" + mboxId + ".mail_item");
/* 150 */     sql.append(" SET blob_digest = ? WHERE id = ? AND type in (");
/* 151 */     sql.append(MailItem.Type.WIKI.toByte());
/* 152 */     sql.append(", ");
/* 153 */     sql.append(MailItem.Type.DOCUMENT.toByte());
/* 154 */     sql.append(")");
/* 155 */     String sqlStr = sql.toString();
/*     */     
/* 157 */     boolean success = false;
/*     */     try {
/* 159 */       for (WikiDigest wd : digests) {
/* 160 */         int id = wd.getItemId();
/* 161 */         String digest = wd.getDigest();
/* 162 */         System.out.println("Setting digest = \"" + digest + "\" for id = " + id);
/* 163 */         PreparedStatement stmt = null;
/*     */         try {
/* 165 */           stmt = conn.prepareStatement(sqlStr);
/* 166 */           int pos = 1;
/* 167 */           stmt.setString(pos++, digest);
/* 168 */           stmt.setInt(pos++, id);
/* 169 */           stmt.executeUpdate();
/*     */         } finally {
/* 171 */           if (stmt != null)
/* 172 */             stmt.close();
/*     */         }
/*     */       }
/* 175 */       success = true;
/*     */     } finally {
/* 177 */       if (success) {
/* 178 */         conn.commit();
/*     */       } else
/* 180 */         conn.rollback();
/*     */     }
/*     */   }
/*     */   
/*     */   public static void main(String[] args) {
/* 185 */     CliUtil.toolSetup("WARN");
/* 186 */     sStore = StoreManager.getInstance();
/* 187 */     DbPool.startup();
/* 188 */     DbPool.DbConnection conn = null;
/*     */     try {
/* 190 */       int numFixed = 0;
/* 191 */       conn = DbPool.getConnection();
/* 192 */       System.out.println("Getting mailbox list...");
/* 193 */       List<Mbox> mboxList = getMboxList(conn);
/* 194 */       for (Mbox m : mboxList) {
/* 195 */         int mboxId = m.getId();
/* 196 */         System.out.println("Processing mailbox " + mboxId + " (" + m.getEmail() + ") ...");
/* 197 */         System.out.println("Getting wiki items needing fixup...");
/* 198 */         List<WikiDigest> digests = getWikiDigests(mboxId);
/* 199 */         int howmany = digests != null ? digests.size() : 0;
/* 200 */         System.out.println("Got " + howmany + " wiki items to fixup.");
/* 201 */         if (howmany > 0) {
/* 202 */           fixupItems(conn, mboxId, digests);
/* 203 */           numFixed += howmany;
/*     */         }
/* 205 */         System.out.println("Done with mailbox " + mboxId + ".");
/*     */       }
/* 207 */       System.out.println("Done.  Fixed " + numFixed + " wiki items.");
/*     */     } catch (Exception e) {
/* 209 */       System.err.println("Error: " + e.getMessage());
/* 210 */       e.printStackTrace(System.err);
/*     */     } finally {
/* 212 */       DbPool.quietClose(conn);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/wiki/WikiDigestFixup.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */